﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Backend.JYD.Model.Bid;
using Backend.JYD.Model;
using Backend.JYD.Model.Cust;

namespace Backend.JYD.BLL.Bid
{
    public class BidStatBLL
    {
        private static readonly IDAL.Bid.IBidStat statIdal = Factory.DALFactory.CreateEFDAL<IDAL.Bid.IBidStat>("DAL.BidStatDAL");

        public BidStat GetBidStat(long userId)
        {
            if (userId <= 0) return null;
            return statIdal.GetBidStatByUserId(userId);
        }

        public IList<BidStat> GetBidStats(long[] userIds)
        {
            IList<BidStat> lstBidStat = null;
            if (userIds != null && userIds.Count() > 0)
            {
                lstBidStat = statIdal.GetBidStatByUserIds(userIds);
            }
            return lstBidStat;
        }

        //投（撤/流）标引起的状态更新
        public bool AddBidUpdate(BidRecord bid, BidStat stat, bool addBid)
        {
            if (stat == null) stat = GetBidStat(bid.UserId);

            if (addBid)
            {
                stat.BiddingNum++;
                stat.BiddingAmount += bid.LoanMoney;
            }
            else
            {
                stat.BiddingNum--;
                stat.BiddingAmount -= bid.LoanMoney;
            }
            return statIdal.UpdateBidStat(stat);
        }

        //开始执行正常还款引起的状态更新
        public bool ExecInviteUpdate(BidRecord bid, BidStat stat)
        {
            if (stat == null) stat = GetBidStat(bid.UserId);

            stat.BiddingNum--;
            stat.BiddingAmount -= bid.LoanMoney;
            stat.TotalCapital -= bid.LoanMoney; //?第三方支付能否同步账户余额?

            stat.PayNum++;
            stat.ComingCapital += bid.LoanMoney;

            decimal interest = PayPlanBLL.TermInterest(bid.LoanMoney, bid.Rate, bid.Term);
            stat.ComingInterest += interest;

            return statIdal.UpdateBidStat(stat);
        }

        //执行还款、逾期、还款结束引起的状态更新
        public bool PayInviteUpdate(BidRecord bid, int oldStatus, bool pay, bool finish)
        {
            BidStat stat = GetBidStat(bid.UserId);

            if (pay)
            {
                decimal interest = PayPlanBLL.TermInterest(bid.LoanMoney, bid.Rate, 1);
                decimal capital = bid.LoanMoney / bid.Term;

                stat.ComingInterest -= interest;
                stat.ComingCapital -= capital;
                stat.TotalCapital += capital + interest;

                if (oldStatus == (int)BidRecordBLL.enumBidStatus.逾期未还) //逾期后充值
                {
                    stat.OverdueNum--;
                    stat.PayNum++;
                }
                if (finish)
                {
                    stat.PayNum--;
                    stat.FinishNum++;
                }
            }
            else
            {
                stat.PayNum--;
                stat.OverdueNum++;
            }
            return statIdal.UpdateBidStat(stat);
        }

        //垫资结束引起的状态更新
        public bool RepayInviteUpdate(BidRecord bid, int repayTerm, PayPlanBLL.enumRepayType repayType)
        {
            BidStat stat = GetBidStat(bid.UserId);

            decimal interest = PayPlanBLL.TermInterest(bid.LoanMoney, bid.Rate, 1);
            decimal capital = bid.LoanMoney / bid.Term;
            int remainTerm = bid.Term - repayTerm + 1;

            if (repayType == PayPlanBLL.enumRepayType.逾期了结 ||
                    repayTerm == bid.Term)
            {
                stat.ComingInterest -= interest * remainTerm;
                stat.ComingCapital -= capital * remainTerm;
                stat.TotalCapital += capital * remainTerm;

                stat.OverdueNum--;
                stat.RepayNum++;
            }
            else
            {
                //持续垫资，预期收益基本不变？
                //如果采用“持续垫资”策略，建议取消“逾期”状态，因为到期必然有网站还款
            }
            return statIdal.UpdateBidStat(stat);
        }
    }
}
